#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QColorDialog>
#include <QFileDialog>
#include <QFontDialog>
#include <QInputDialog>
#include <QMessageBox>
#include <QProgressDialog>
#include <QTextStream>
#include <QtPrintSupport/QPageSetupDialog>
#include <QtPrintSupport/QPrintDialog>
#include <QtPrintSupport/QPrintPreviewDialog>

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    QString ds = QString( "'E' \nformat, precision 3,\n gives %1" ).arg( 20.10345, 0, 'f', 2);
    qDebug()<<ds;
    ui->setupUi(this);
    ui->labelTest->setText(ds);
    qDebug()<<QString("%1%2").arg(20.12345, 0, 'f', 2).arg("\ntest");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionColor_triggered()
{
    QColorDialog dlg(this);
    dlg.exec();
}

void MainWindow::on_actionFile_triggered()
{
    QString path = QFileDialog::getOpenFileName(this, tr("Open File"), ".", tr("Text Files(*.txt)"));

    if (path.isEmpty()) {
        return;
    }

    QFile file(path);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        return;
    }

    QTextStream in(&file);
    ui->textEdit->setText(in.readAll());

    file.close();
}

void MainWindow::on_actionFont_triggered()
{
    QFontDialog dlg(this);
    dlg.exec();
}

void MainWindow::on_actionInput_triggered()
{
    QInputDialog dlg(this);
    dlg.exec();
}

void MainWindow::on_actionMessage_triggered()
{
    QMessageBox::information(this, tr("Information"), tr("It's a information dialog"));
}

void MainWindow::on_actionPageSetup_triggered()
{
    QPageSetupDialog dlg(this);
    dlg.exec();
}

void MainWindow::on_actionPrint_triggered()
{
    QPrintDialog dlg(this);
    dlg.exec();
}

void MainWindow::on_actionPrintPreview_triggered()
{
    QPrintPreviewDialog dlg(this);
    dlg.exec();
}

void MainWindow::on_actionProgress_triggered()
{
    QProgressDialog dlg(this);
    dlg.exec();
}

void MainWindow::on_actionAbout_triggered()
{
    QMessageBox::about(this, tr("About"), tr("It's a about dialog"));
//    QMessageBox::aboutQt(this, tr("AboutQt"));
}

void MainWindow::on_actionError_triggered()
{
    QMessageBox::critical(this, tr("Error"), tr("It's a critical dialog"));
}

void MainWindow::on_actionHelpQuestion_triggered()
{
    QMessageBox::question(this, tr("Question"), tr("Are you OK?"), QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
}

void MainWindow::on_actionWarning_triggered()
{
    QMessageBox::warning(this, tr("Warning"), tr("It's a warning dialog"), QMessageBox::Ok);
}

void MainWindow::on_actionSave_triggered()
{
    QString path = QFileDialog::getSaveFileName(this, tr("Save File"), ".", tr("Text Files(*.txt)"));

    if (path.isEmpty()) {
        return;
    }

    QFile file(path);
    if (!file.open(QIODevice::WriteOnly|QIODevice::Text)) {
        return;
    }

    QTextStream out(&file);
    out<<ui->textEdit->toPlainText();
    file.close();
}
